﻿/* Copyright (c) 2015 Slava Naumenko - http://m4w.codeplex.com */

using System;
using System.Web;
using System.Web.Services;

namespace M4W.ASM.Services
{
    [WebService(Namespace = "M4W.ASM.Services")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    [System.ComponentModel.ToolboxItem(false)]
    public class Text : System.Web.Services.WebService
    {
        [WebMethod]
        public string CleanTitle(string Title)
        {
            string result = "";
            try
            {
                result = TextTransform.Invoke(Title, "CleanTitle");
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }

        [WebMethod]
        public string CleanUrl(string Url)
        {
            string result = "";
            try
            {
                result = TextTransform.Invoke(Url, "CleanUrl");
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }

        [WebMethod]
        public string EncodeUrl(string Url)
        {
            string result = "";
            try
            {
                result = TextTransform.Invoke(Url, "EncodeUrl");
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }

        [WebMethod]
        public string FirstWord(string Text)
        {
            string result = "";
            try
            {
                result = TextTransform.Invoke(Text, "FirstWord");
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }

        [WebMethod]
        public string LastWord(string Text)
        {
            string result = "";
            try
            {
                result = TextTransform.Invoke(Text, "LastWord");
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }
    }

    // Invocations

    internal static class TextTransform
    {
        internal static string Invoke(string str, string transform)
        {
            string result = "";

            switch (transform)
            {
                case "CleanTitle":
                    result = str.Trim().Replace("'", "").Replace("\"", "").Replace("*", "").Replace("~", "").Replace("!", "").Replace("%", "").Replace("^", "").Replace("@", "at");
                    break;
                case "CleanUrl":
                    result = str.Trim().Replace("'", "").Replace("\"", "").Replace("*", "").Replace("~", "").Replace("!", "").Replace("%", "").Replace("^", "").Replace("&", "n").Replace("@", "at").Replace("-", "_").Replace("+", "_").Replace(".", "_").Replace(" ", "_").Replace("__", "_").Replace("__", "_");
                    break;
                case "EncodeUrl":
                    result = HttpUtility.UrlEncode(str).Replace(".", "%2E");
                    break;
                case "FirstWord":
                    int ind = -1;
                    int ind1 = str.IndexOf(",");
                    int ind2 = str.IndexOf(" ");
                    if (ind1 != -1 && ind2 != -1 && ind1 < ind2) { ind = ind1; }
                    else if (ind1 != -1 && ind2 != -1 && ind2 < ind1) { ind = ind2; }
                    else if (ind1 != -1) { ind = ind1; }
                    else if (ind2 != -1) { ind = ind2; }
                    if (ind != -1) { result = str.Substring(0, ind); } else { result = str; }
                    break;
                case "LastWord":
                    int ind3 = str.LastIndexOf(" ");
                    if (ind3 != -1) { result = str.Substring(ind3); } else { result = str; }
                    break;
                default:
                    throw new Exception("'" + transform + "' is not implemented.");
            }

            return result;
        }
    }
}